﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.Basic.Model;
using System.Collections.ObjectModel;
using System.Collections;

namespace EIA.SLClient.PlatManage.PMMgr
{
    public class SR_StaffListViewModel : SimpleMvvmToolkit.ViewModelBase<SR_StaffListViewModel>
    {
        public SR_StaffListViewModel()
        {

        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.SR_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.SR_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected PagedCollectionView _StaffCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public PagedCollectionView StaffCollection
        {
            get { return _StaffCollection; }
            set
            {
                _StaffCollection = value;
                NotifyPropertyChanged(m => m.StaffCollection);
                OnFilter();
            }
        }

        protected IEnumerable _StaffsSelected = null;
        /// <summary>
        /// 注释
        /// </summary>
        public IEnumerable StaffsSelected
        {
            get { return _StaffsSelected; }
            set
            {
                _StaffsSelected = value;
                NotifyPropertyChanged(m => m.StaffsSelected);

                IList<TStaff> lst = null;
                if (value != null) lst = value.Cast<TStaff>().ToList();
                SendMessage<IList<TStaff>>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.SR_STAFFS_SELECTED, new SimpleMvvmToolkit.NotificationEventArgs<IList<TStaff>>("", lst));
            }
        }

        protected bool _ShowDeactive;
        /// <summary>
        /// 是否显示注销用户
        /// </summary>
        public bool ShowDeactive
        {
            get { return _ShowDeactive; }
            set
            {
                _ShowDeactive = value;
                NotifyPropertyChanged(m => m.ShowDeactive);
                OnFilter();
            }
        }

        private void OnFilter()
        {

            if (StaffCollection != null)
            {
                if (ShowDeactive)
                    StaffCollection.Filter = null;
                else
                    StaffCollection.Filter = (staff) => { return (staff as EIA.Basic.Model.TStaff).IsActive != 0; };
            }
        }

        #endregion

        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            EIA.Basic.BLL.ClientProxy.TStaffProxy proxy = new EIA.Basic.BLL.ClientProxy.TStaffProxy();
            Busy();
            proxy.ListAll((lst) =>
            {
                Free();
                StaffCollection = new PagedCollectionView(lst);

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取员工列表发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            if (StaffCollection == null) RetrieveItems();
        }

    }
}